home *** CD-ROM | disk | FTP | other *** search
- program TrieTest;
-
- {$IFDEF VER80}
- !! Error
- This program is for long strings only. In other words, you must be
- using Delphi 2 or later. The reason is that the trie assumes that the
- string is terminated by a null.
- {$ENDIF}
-
- {$APPTYPE CONSOLE}
-
- uses
- Windows,
- SysUtils,
- Classes,
- Trie in 'trie.pas',
- Ternary in 'Ternary.pas';
-
-
- {===Stream:get next word=============================================}
- function GetNextWord(Strm : TStream) : string;
- var
- Ch : char;
- begin
- Result := '';
- with Strm do begin
- Read(Ch, 1);
- while not (Ch in [#0, 'a'..'z', 'A'..'Z', '0'..'9']) do
- Read(Ch, 1);
- while Ch in ['a'..'z', 'A'..'Z', '0'..'9'] do begin
- Result := Result + Ch;
- Read(Ch, 1);
- end;
- end;
- end;
- {====================================================================}
-
- procedure WritelnAction(const S : string; Data : pointer);
- begin
- writeln(S);
- end;
- procedure WriteAction(const S : string; Data : pointer);
- begin
- write(S, ' ');
- end;
-
-
- var
- MyTrie : TTrie;
- TernTree : TTernaryTree;
- StringList : TStringList;
- Obj : pointer;
- Strm : TMemoryStream;
- Ch : char;
- NextWord : string;
- StartTime, EndTime : integer;
- TotalSize : integer;
- Count : integer;
- begin
- Strm := TMemoryStream.Create;
- try
- Strm.LoadFromFile('HENRYV.TXT');
- Strm.Position := Strm.Size;
- Ch := #0;
- Strm.Write(Ch, 1);
- Strm.Position := 0;
- StringList := TStringList.Create;
- try
- StringList.Sorted := true;
- NextWord := GetNextWord(Strm);
- while (NextWord <> '') do begin
- StringList.AddObject(NextWord, @MyTrie);
- NextWord := GetNextWord(Strm);
- end;
- StartTime := GetTickCount;
- for Count := 1 to 300 do begin
- Strm.Position := 0;
- NextWord := GetNextWord(Strm);
- while (NextWord <> '') do begin
- if (StringList.IndexOf(NextWord) = -1) then
- writeln('Error, didn''t find [', NextWord, ']');
- NextWord := GetNextWord(Strm);
- end;
- end;
- EndTime := GetTickCount;
- finally
- StringList.Free;
- end;
- writeln('StringList time: ', EndTime-StartTime);
-
- Strm.Position := 0;
- MyTrie := TTrie.Create;
- try
- NextWord := GetNextWord(Strm);
- while (NextWord <> '') do begin
- MyTrie.AddObject(NextWord, @MyTrie);
- NextWord := GetNextWord(Strm);
- end;
- StartTime := GetTickCount;
- for Count := 1 to 300 do begin
- Strm.Position := 0;
- NextWord := GetNextWord(Strm);
- while (NextWord <> '') do begin
- if not MyTrie.FindString(NextWord, Obj) then
- writeln('Error, didn''t find [', NextWord, ']');
- NextWord := GetNextWord(Strm);
- end;
- end;
- EndTime := GetTickCount;
- TotalSize := MyTrie.NodeCount * sizeof(TTrieNode);
- finally
- MyTrie.Free;
- end;
- writeln('Trie time: ', EndTime-StartTime);
- writeln('Trie size: ', TotalSize);
-
- Strm.Position := 0;
- TernTree := TTernaryTree.Create;
- try
- TernTree.IgnoreCase := true;
- NextWord := GetNextWord(Strm);
- while (NextWord <> '') do begin
- if not TernTree.Search(NextWord, Obj) then
- TernTree.Insert(NextWord, @MyTrie);
- NextWord := GetNextWord(Strm);
- end;
- StartTime := GetTickCount;
- for Count := 1 to 300 do begin
- Strm.Position := 0;
- NextWord := GetNextWord(Strm);
- while (NextWord <> '') do begin
- if not TernTree.Search(NextWord, Obj) then
- writeln('Error, didn''t find [', NextWord, ']');
- NextWord := GetNextWord(Strm);
- end;
- end;
- EndTime := GetTickCount;
- TernTree.Iterate(WriteAction); writeln;
- TernTree.PartialSearch('....e', WriteAction); writeln;
- TernTree.PartialSearch('...e.', WriteAction); writeln;
- TernTree.PartialSearch('..e..', WriteAction); writeln;
- TernTree.PartialSearch('.e...', WriteAction); writeln;
- TernTree.PartialSearch('e....', WriteAction); writeln;
- finally
- TernTree.Free;
- end;
- writeln('ternary Tree time: ', EndTime-StartTime);
-
- finally
- Strm.Free;
- end;
- readln;
- end.
-
-